package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ManageServiceImpl  implements ManageService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SpuPosterMapper spuPosterMapper;



    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<>();
        wrapper.eq("category1_id", category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);
        return baseCategory2List;
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id", category2Id);
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrapper);
        return baseCategory3List;
    }

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.selectAttrInfoList(category1Id, category2Id, category3Id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //如果不等与空 做修改操作
        if (baseAttrInfo.getId() != null) {
            baseAttrInfoMapper.updateById(baseAttrInfo);
        } else {
            baseAttrInfoMapper.insert(baseAttrInfo);
        }

        //修改： 先删除baseAttrValue 再新增
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", baseAttrInfo.getId());
        baseAttrValueMapper.delete(wrapper);

        //获取页面传递过来的平台所有属性值
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)) {
            for (BaseAttrValue baseAttrValue : attrValueList) {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {


        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        return baseAttrValueMapper.selectList(wrapper);
    }

    @Override
    public BaseAttrInfo getBaseAttrInfo(Long attrId) {
        //获取平台属性对象
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        if (baseAttrInfo != null) {

            baseAttrInfo.setAttrValueList(getAttrValueList(attrId));
        }
        return baseAttrInfo;
    }

    @Override
    public IPage<SpuInfo> getSupInfoPage(Page<SpuInfo> spuInfoPage, SpuInfo spuInfo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category3_id",spuInfo.getCategory3Id());
        wrapper.orderByDesc("id");

        return spuInfoMapper.selectPage(spuInfoPage,wrapper);
    }

    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //获取主键自增
        spuInfoMapper.insert(spuInfo);

        //spuImage
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if(!CollectionUtils.isEmpty(spuImageList)){
            spuImageList.stream().forEach(spuImage -> {
                //加入spu_id后再增加
                Long id = spuInfo.getId();
                spuImage.setSpuId(id);
                spuImageMapper.insert(spuImage);
            });
        }
        //spuPoster
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuImageList)){
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuInfo.getId());
                spuPosterMapper.insert(spuPoster);
            });
        }

        //  获取销售属性
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)){
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuInfo.getId());
                spuSaleAttrMapper.insert(spuSaleAttr);
                //从销售属性获取销售属性值
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if(!CollectionUtils.isEmpty(spuSaleAttrValueList)){
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    });
                }
            });

        }
    }

    @Override
    public SpuInfo getSupInfo(Long id) {
        SpuInfo spuInfo = spuInfoMapper.selectById(id);

        List<SpuImage> spu_image = spuImageMapper.selectList(new QueryWrapper<SpuImage>().eq("spu_id", id));

        List<SpuPoster> sup_poster = spuPosterMapper.selectList(new QueryWrapper<SpuPoster>().eq("spu_id", id));

        List<SpuSaleAttr> spu_attr = spuSaleAttrMapper.selectList(new QueryWrapper<SpuSaleAttr>().eq("spu_id", id));

        spu_attr.stream().forEach(spuSaleAttr -> {
            QueryWrapper<SpuSaleAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("spu_id",id);
            wrapper.eq("sale_attr_name",spuSaleAttr.getSaleAttrName());
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttrValueMapper.selectList(wrapper);
            spuSaleAttr.setSpuSaleAttrValueList(spuSaleAttrValueList);
        });
       spuInfo.setSpuImageList(spu_image);
       spuInfo.setSpuPosterList(sup_poster);
       spuInfo.setSpuSaleAttrList(spu_attr);

        return spuInfo;
    }

    @Override
    public void updateSpuInfo(SpuInfo spuInfo) {
        //TODO 未完成
        if (spuInfo!=null && spuInfo.getSpuPosterList()!=null){
            QueryWrapper<SpuImage> wrapper = new QueryWrapper<>();
            wrapper.eq("spu_id",spuInfo.getId());
            List<SpuImage> spuImages = spuImageMapper.selectList(wrapper);

            QueryWrapper<SpuPoster> spuPosterQueryWrapper = new QueryWrapper<>();
            spuPosterQueryWrapper.eq("spu_id",spuInfo.getId());
            List<SpuPoster> spuPosters = spuPosterMapper.selectList(spuPosterQueryWrapper);

            QueryWrapper<SpuSaleAttr> spuSaleAttrQueryWrapper = new QueryWrapper<>();
            spuSaleAttrQueryWrapper.eq("spu_id",spuInfo.getId());
            List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrMapper.selectList(spuSaleAttrQueryWrapper);
            spuSaleAttrs.forEach(spuSaleAttr -> {
                QueryWrapper<SpuSaleAttrValue> spuSaleAttrValueQueryWrapper = new QueryWrapper<>();
                wrapper.eq("spu_id",spuInfo.getId());
                wrapper.eq("sale_attr_name",spuSaleAttr.getSaleAttrName());
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttrValueMapper.selectList(spuSaleAttrValueQueryWrapper);
                spuSaleAttr.setSpuSaleAttrValueList(spuSaleAttrValueList);
            });
            spuInfo.setSpuImageList(spuImages);
            spuInfo.setSpuPosterList(spuPosters);
            spuInfo.setSpuSaleAttrList(spuSaleAttrs);

           /* SpuImage spuImage = spuImageMapper.selectById(spuInfo);
            SpuPoster spuPoster = spuPosterMapper.selectById(spuInfo);
            SpuSaleAttr spuSaleAttr = spuSaleAttrMapper.selectById(spuInfo);
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            */
            spuInfoMapper.updateById(spuInfo);
        }
        else return;
    }
}
